{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6aa58057",
   "metadata": {},
   "source": [
    "---------------  \n",
    "* 时间： 2022/10/31  \n",
    "* 课程： Python  \n",
    "* 记录人: 江柯廷  \n",
    "---------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f04cbd6a",
   "metadata": {},
   "source": [
    "# 本周知识点  \n",
    "  \n",
    "* 元组、集合和字典的基本概念和常见的方法  \n",
    "\n",
    "> 1. 元组  \n",
    "> 2. 集合  \n",
    "> 3. 字典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e684c777",
   "metadata": {},
   "source": [
    "## 1. 元组（tuple）  \n",
    "> 1. 概念  \n",
    ">> 1. 具有和序列数据相同的运算规则：取值和切片  \n",
    ">> 2. 表征: (12345,54321,'hello'),用圆括号/小括号 括起来的,中间仍然是用逗号隔开的数据形态  \n",
    "> 2. 性质: ***不可变性***,没有什么增删改的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "35e82429",
   "metadata": {},
   "outputs": [],
   "source": [
    "product = (\"Head First Python\",88) # 创建了一个元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a2985652",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Head First Python', 88) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "print(product,type(product))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5738c5da",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 案例  \n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9449bfbc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2022, tm_mon=10, tm_mday=31, tm_hour=10, tm_min=20, tm_sec=24, tm_wday=0, tm_yday=304, tm_isdst=0)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "50606783",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function localtime in module time:\n",
      "\n",
      "localtime(...)\n",
      "    localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,\n",
      "                              tm_sec,tm_wday,tm_yday,tm_isdst)\n",
      "    \n",
      "    Convert seconds since the Epoch to a time tuple expressing local time.\n",
      "    When 'seconds' is not passed in, convert the current time instead.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(time.localtime)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "05d912eb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1800001, 1800002, 1800003)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_seq = 1800001,1800002,1800003  \n",
    "name_seq"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3de07bb9",
   "metadata": {},
   "source": [
    "## 2. 集合(sets)  \n",
    "> 1. 集合的概念  \n",
    ">> 1. 没有重复元素的数据结构  \n",
    ">> 2. 形态:{'apple','orange','apple','pear','orange','banana'}  \n",
    "> 2. 集合的运算  \n",
    ">> 1. 交集运算:    &     ,intersection  \n",
    ">> 2. 并集运算:   |    ,union"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "909db937",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'orange', 'apple', 'banana', 'pear'} <class 'set'>\n"
     ]
    }
   ],
   "source": [
    "basket = {'apple','orange','apple','pear','orange','banana'}\n",
    "print(basket,type(basket))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c5d3e1f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "舞蹈社 = {1801,1802,1803,1804,1809}\n",
    "合唱团 = {1806,1802,1807,1805,1809}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "9e8d6642",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(舞蹈社)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "13630a10",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(合唱团)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ebfd5801",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 交集运算和并集运算的实践  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "200e23a9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1809, 1802}\n",
      "{1809, 1802}\n"
     ]
    }
   ],
   "source": [
    "# 交集  \n",
    "print(舞蹈社&合唱团)  \n",
    "print(舞蹈社.intersection(合唱团))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "0d87a4a5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1801, 1802, 1803, 1804, 1805, 1806, 1807, 1809}\n",
      "8\n",
      "{1801, 1802, 1803, 1804, 1805, 1806, 1807, 1809}\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "# 并集,统计数量  \n",
    "print(舞蹈社|合唱团)\n",
    "print(len(舞蹈社|合唱团))\n",
    "\n",
    "print(舞蹈社.union(合唱团))\n",
    "print(len(舞蹈社.union(合唱团)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15fe6e1d",
   "metadata": {},
   "source": [
    "## 3. 字典(dict)   \n",
    "> 1. 基本的概念  \n",
    ">> 1. mapping types :映射类型  \n",
    ">> 2. key:values,键:值 对  \n",
    ">> 3. 字典的性质  \n",
    ">>> 1. 无序性  \n",
    ">>> 2. key不可变性  \n",
    ">>> 3. key的唯一性  \n",
    "> 2. 字典的创建  \n",
    "\n",
    "> 3. 常用的方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "812af7b7",
   "metadata": {},
   "source": [
    "product_list = [  \n",
    "    [\"蜜雪\",6],  \n",
    "    [\"蜜桃\",10],  \n",
    "    [\"芒果\",12],  \n",
    "    [\"葡萄\",8]  \n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "5725e2b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字典的创建\n",
    "product_dict = [\n",
    "    [\"蜜雪\",6],\n",
    "    [\"蜜桃\",10],\n",
    "    [\"芒果\",12],\n",
    "    [\"葡萄\",8]\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "8b3f5782",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['蜜雪', 6], ['蜜桃', 10], ['芒果', 12], ['葡萄', 8]] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "print(product_dict,type(product_dict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "7152c0bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['蜜雪', 6]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "product_dict[0] # 字典是无序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "15c9c478",
   "metadata": {},
   "outputs": [],
   "source": [
    "user_info = {\n",
    "    \"tingyu_work\":[\"tingyu\",\"IT\",\"广州\",\"通讯录\",\"朋友圈\"]\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "4be738f0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['tingyu', 'IT', '广州', '通讯录', '朋友圈']"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info[\"tingyu_work\"]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
